package com.beiding.workordersystem.common.handler;

import com.beiding.workordersystem.common.exception.BaseException;
import com.beiding.workordersystem.common.standard.R;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Component
public class DefaultExceptionHandler {

    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(HttpStatus.OK)
    public R<String> accessDeniedException(AccessDeniedException e) {
        return R.forbidden("无权操作");
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public R handlerException(Exception e) {
        if (!(e instanceof BaseException)) {
            e.printStackTrace();
        }
        return R.error(e);
    }
}
